// pages/userInfoCenter/index.js
const App = getApp();
const {Dialog, Toast} = require("../../../../../config/modal")
const utils = require("../../../../../utils/util")
Page({
    /**
     * 页面的初始数据
     */
    data: {
        theamTextColor: App.globalData.theamTextColor,
        title:"消息通知",
        loadMore: {
            show: false,
            type: "loading",
            text: "加载中..."
        },
        message: {
            list: [],
        },

        page: {
            pageNum: 1,
            pageSize: 15,
            pages: 0,
        },

    },
    URL: {
        getList: "/message/pageList",
        read: "/message/read/?",
    },
    page: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.hideShareMenu();
        this.init(this.data.page.pageNum, false);
    },
    onShow() {
    },

    // 初始化地址列表
    init(pageNum, loadMore) {
        let params = {
            pageNum: pageNum,
            pageSize: this.data.page.pageSize,
        }
        this.setLoadingMore(loadMore, "loading", "加载中...")
        App.$get(!loadMore ? true : loadMore, this.URL.getList, params, res => {
            this.setData({
                "loadMore.show": false,
            })
            if (res.R) {
                this.setData({
                    "page.pageNum": res.data.pageNum,
                    "page.pageSize": res.data.pageSize,
                    "page.pages": res.data.pages
                })
                if (!loadMore) {
                    this.setData({
                        "message.list": res.data.rows,
                    })
                    this.roRead(res.data.rows)
                    return;
                }
                let sourceData = this.data.message.list;
                let newData = res.data.rows;
                newData.forEach(data => {
                    sourceData.push(data);
                })
                this.setData({
                    "message.list": sourceData,
                })
                this.roRead(res.data.rows)
            }
        })
    },

    // 返回上一级
    goBack() {
        wx.navigateBack();
    },

    roRead(rows){
        let ids=[];
        for (let i = 0; i <rows.length; i++) {
            let e=rows[i];
            if (!e.read){
                ids.push(e.id)
            }
        }
        if(ids.length<=0){
            return;
        }
        App.$postIsTip(false,false,this.URL.read.replace("?",ids),{},res=>{})
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.page.pages === 0 || this.data.page.pageNum + 1 > this.data.page.pages) {
            this.setLoadingMore(true, "end", "我是用底线的~")
            return;
        }
        this.init(this.data.page.pageNum + 1, true);
    },
    setLoadingMore(show, type, text) {
        this.setData({
            "loadMore.show": show,
            "loadMore.type": type,
            "loadMore.text": text,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
